/**
 * \file appOTv2.c
 * \brief Routine de l'OS
 * \author Pierrick.C
 * \version 0.1
 * \date 11 septembre 2007
 *
 * Fichier où les taches sont crées, l'horloge et l'OS lancés
 *
 */

/*
*********************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*
*                              (c) Copyright 2002, Micrium, Inc., Weston, FL
*                                           All Rights Reserved
*
*                                                TI MSP430
*********************************************************************************************************
*/

int global_pb_gd=0;  // a voir si utile
#include 	<includes.h>
#include 	<msp430x14x.h> 
#include 	<signal.h>
#include	<radio.h> // include des d�finition des taches


/**
 * \def TASK_STK_SIZE
 * Taille des piles pour chaque tache
 * 
 */
#define  TASK_STK_SIZE                  64       /* Size of each task's stacks (# of OS_STK entries)   */


/* Declaration des piles pour chaque tache*/

//OS_STK   StkTestDisplay[TASK_STK_SIZE];
//OS_STK   StkTestLED[TASK_STK_SIZE];
//OS_STK   StkTestFreq[TASK_STK_SIZE];
//OS_STK   StkTestVolume[TASK_STK_SIZE];
//OS_STK   StkTestMem[TASK_STK_SIZE];

OS_STK   MajStatsStk[TASK_STK_SIZE];
OS_STK   DisplayStk[TASK_STK_SIZE];
OS_STK   BipperStk[TASK_STK_SIZE];

OS_STK   StartStk[TASK_STK_SIZE];


/**
 * \fn void initOsc()
 * \brief Initialise l'horloge
 * 
 * Initialise le time tick à 10ms
 * 
 */
 
void initOsc()
{
//Intialisation du quartz
	WDTCTL = WDTPW | WDTHOLD;                      // stop watchdog timer

	BCSCTL1 |= XTS;                                // XT1 as high-frequency
	_BIC_SR(OSCOFF);                               // turn on XT1 oscillator

	do                                             // wait in loop until crystal is stable
		IFG1 &= ~OFIFG;
	while (IFG1 & OFIFG);
	
	// Initialisation de la clock externe (ACLK) à 1MHz
	BCSCTL1 |= DIVA_DIV8 ;				      		//ACLK = LFXT1CLK (crystal) division par 8
	
	
	//intialisaton de la master clock (MCLK)
	
	BCSCTL2 |= SELM_3;      		// MCLK = LFXT1 = CRYS(safe)
  

	//Intialisation du watch dog 
	IE1 &= ~WDTIE;                                 // disable WDT int.
	IFG1 &= ~WDTIFG;                               // clear WDT int. flag

	WDTCTL = WDTPW | WDTTMSEL | WDTCNTCL | WDTSSEL | WDTIS1; // use WDT as timer, flag each
                                                           // 512 pulses from ACLK
		
	while (!(IFG1 & WDTIFG));                      // count 1024 pulses from XT1 (until XT1's
                                                 // amplitude is OK)

	IFG1 &= ~OFIFG;                                // clear osc. fault int. flag
	

	
	// config timer B
	/*
	
	TBCTL = TBSSEL_1 + TBCLR; // source clock ACLK + reset(clock divider, count direction)
	TBCTL |= 0x00C0; // Division par 8 en entrée
	
	TBCCR0= 62500-1; //500ms	
	TBCCTL0 = CCIE; //CCR0 interrupt eneable
	*/


	// config timer A
	
	TACTL    = TASSEL1 + TACLR;         /* Clear the Timer and set SMCLK as the source. */
    TACTL   |= 0x00C0;                  /* Input divider is /8.  */
    TACCTL0  = CCIE;                    /* Enable the TACCR0 interrupt. */
    TACCR0   = 1000;                   /* Load the TACCR0 register.   TICKTIME de 10ms */

	
}

/**
 * \fn void initPort()
 * \brief Initialise les ports
 * 
 * Initialise les ports commun à plusieurs drivers
 *
 */
 
void initPort()
{
	// Initialisation des ports
	P1SEL = 0;                                     //
	P1OUT = 0;                                     //
	P1IN  = 0x00;				//Initialisation de P1IN � 0
	P1OUT = 0x00;				//Initialisation de P1OUT � 0
	P1DIR = 0x00;				//P1 en IN 
	P1DIR = BIT5 | BIT6;                           //enable only Relay outputs P1.5 et P1.6 en OUT
	
	P2SEL = 0;
	P2OUT = 0;
	P2DIR=~BIT0;   //DEJA DANS UNE AUTRE INIT DE DRIVER
	
	P3SEL |= BIT4 | BIT5;                         //enable UART0
	P3DIR |= BIT4;                                //enable TXD0 as output
	P3DIR &= ~BIT5;                               //enable RXD0 as input
	
	P4SEL = 0;
	P4OUT = 0;
	P4DIR = BIT2 | BIT3;                          //only buzzer pins are outputs
	
	P6SEL = 0;
	P6OUT = 0;
	P6DIR = 0xff;                                  // all output
	P6OUT |= BIT7;					//Placement en mode volume par defaut.	


	P1IFG = 0x00;				//Initialisation des flags d'interruptions a 0
	P1IES = 0xFF;				//Interruption sur un front descendant
	P1IE  = 0xFF;				//P1 interrupt enable
	P1IFG = 0x00;
	
}



int  main (void)
{
	
    initOsc();
    initPort(); //intialisation des ports communs 
    couperSon();
    OSInit();               /* Initialize uC/OS-II                      */
    initRessource();		//intialize les semaphores et les boites au lettre
    
    /*
     * ********************************************************************************
     *								Creation tache test
     * *******************************************************************************/
     
	//OSTaskCreate(timer, (void*)5, &StkTimer[TASK_STK_SIZE - 1], 4); //demarre les timers

	//OSTaskCreate(testDisplay, (void *)4, &StkTestDisplay[TASK_STK_SIZE - 1], 4);  // tache de test de l'affichage
	//OSTaskCreate(testLED, (void *)4, &StkTestLED[TASK_STK_SIZE - 1], 4);
	//OSTaskCreate(testFreq, (void *)4, &StkTestFreq[TASK_STK_SIZE - 1], 4);
	//OSTaskCreate(testVolume, (void *)4, &StkTestVolume[TASK_STK_SIZE - 1], 4);
	//OSTaskCreate(testMem, (void *)4, &StkTestMem[TASK_STK_SIZE - 1], 4);
	/*

    OSTaskCreate(start, (void *)4, &StkTestLED[TASK_STK_SIZE - 1], 4);
    OSTaskCreate(display, (void *)5, &DisplayStk[TASK_STK_SIZE - 1], 5);
    OSTaskCreate(startRemote, (void *)5, &IrdaStk[TASK_STK_SIZE - 1], 5);*/

    
 
    /*
    *************************************************************************************************
	*                                Tache principale
	***************************************************************************************************/
    OSTaskCreate(display, (void *)7, &DisplayStk[TASK_STK_SIZE - 1], 7);
    OSTaskCreate(start, (void *)6, &StartStk[TASK_STK_SIZE - 1], 6);
    OSTaskCreate(buttonBipper, (void *)5, &BipperStk[TASK_STK_SIZE - 1], 5);
    OSTaskCreate(majStats, (void *)4, &MajStatsStk[TASK_STK_SIZE - 1], 4);

   
    
	
    OSStart();                                             /* Start multitasking                       */

}





 

